import { get } from '../utils/request';

/**
 * 直接测试OSS API接口
 */
export const testOssApiDirectly = async () => {
  console.log('🧪 开始直接测试OSS API...');

  try {
    const response = await get('/oss/get_torrent');
    console.log('📡 原始API响应:', response);
    console.log('📊 响应类型:', typeof response);
    console.log('🔍 是否为数组:', Array.isArray(response));

    if (Array.isArray(response)) {
      console.log('✅ API直接返回数组格式');
      console.log('📈 数组长度:', response.length);

      if (response.length > 0) {
        console.log('🔎 第一个元素字段:', Object.keys(response[0]));
        console.log('🗂️ 第一个元素内容:', response[0]);
      }
    } else if (response && typeof response === 'object') {
      console.log('📦 API返回对象格式');
      console.log('🔑 对象字段:', Object.keys(response));

      if (response.code !== undefined) {
        console.log('📄 包装格式，code:', response.code);
        console.log('💬 message:', response.message);
        console.log('📊 data:', response.data);
      }
    }

    return response;
  } catch (error) {
    console.error('❌ OSS API测试失败:', error);
    throw error;
  }
};

// 自动测试（在URL包含test=oss时）
if (window.location.search.includes('test=oss')) {
  console.log('🚀 自动运行OSS API测试...');
  testOssApiDirectly().then(result => {
    console.log('✅ 测试完成，结果:', result);
    // 将结果显示在页面上
    const testResult = document.createElement('div');
    testResult.style.cssText = `
      position: fixed; top: 10px; right: 10px; 
      background: #f0f0f0; padding: 10px; border: 1px solid #ccc;
      max-width: 400px; max-height: 300px; overflow: auto;
      z-index: 9999; font-family: monospace; font-size: 12px;
    `;
    testResult.innerHTML = `
      <strong>OSS API测试结果:</strong><br/>
      类型: ${Array.isArray(result) ? '数组' : typeof result}<br/>
      ${Array.isArray(result) ? `长度: ${result.length}` : `字段: ${Object.keys(result || {}).join(', ')}`}
      <button onclick="this.parentElement.remove()" style="float: right;">关闭</button>
    `;
    document.body.appendChild(testResult);
  }).catch(error => {
    console.error('测试失败:', error);
  });
}

export default testOssApiDirectly; 